package com.chw.admin.order;
 
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chw.admin.user.SysUserController;
import com.chw.annotation.PermissionSecurity;
import com.chw.common.ServiceData;
import com.chw.utils.LogUtil;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;

@Controller
@RequestMapping({ "/order" })
public class ChwOrderController {
	private static LogUtil log = LogUtil.newInstance(ChwOrderController.class);

	@Autowired
	private IChwOrderService chwOrderService;

	@RequestMapping(value = { "/find.do" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST }, consumes = {
					"*/*" }, produces = { "application/json" })
	@ResponseBody
	@PermissionSecurity(name = "查询", module = "订单管理", btnfunc = "find")
	public ServiceData find(HttpServletRequest request, ChwPoHeader chwPoHeader, Integer currentPage, Integer pageSize)
			throws Exception {
		ServiceData ret = new ServiceData();
		PageInfo<ChwPoHeader> chw = this.chwOrderService.findEntity(chwPoHeader, currentPage, pageSize);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(chw);
		return ret;
	}

	@RequestMapping(value = { "/findChwPoLine.do" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST }, consumes = {
					"*/*" }, produces = { "application/json" })
	@ResponseBody
	@PermissionSecurity(name = "查询", module = "订单管理", btnfunc = "findChwPoLine")
	public ServiceData findChwPoLine(HttpServletRequest request, ChwPoLine chwPoLine, Integer currentPage, Integer pageSize)
			throws Exception {
		// 查询订单行信息
		ServiceData ret = new ServiceData();
		PageInfo<ChwPoLine> chw = this.chwOrderService.findChwPoLine(chwPoLine, currentPage, pageSize);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(chw);
		return ret;
	}

	@RequestMapping(value = { "/getEntityById.do" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST }, consumes = {
					"*/*" }, produces = { "application/json" })
	@ResponseBody
	@PermissionSecurity(name = "根据id查询", module = "订单管理", btnfunc = "getEntityById")
	public ServiceData getEntityById(HttpServletRequest request, String id) throws Exception {
		ServiceData ret = new ServiceData();
		ChwOrder chw = this.chwOrderService.getEntityById(id);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(chw);
		return ret;
	}
 
	@RequestMapping(value = { "/save.do" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST }, consumes = {
					"*/*" }, produces = { "application/json" })
	@ResponseBody 
	@PermissionSecurity(name = "新增", module = "订单管理", btnfunc = "save")
	public ServiceData save(HttpServletRequest request, String param) throws Exception {
		ServiceData ret = new ServiceData();
		Gson gson = new Gson();
		ChwOrder chwOrder = (ChwOrder) gson.fromJson(param, ChwOrder.class);
		int i = this.chwOrderService.addEntity(chwOrder.getChwPoHeader(), chwOrder.getLineList());
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(Integer.valueOf(i));
		return ret;
	}

	@RequestMapping(value = { "/update.do" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST }, consumes = {
					"*/*" }, produces = { "application/json" })
	@ResponseBody
	@PermissionSecurity(name = "修改", module = "订单管理", btnfunc = "update")
	public ServiceData update(HttpServletRequest request, String param) throws Exception {
		ServiceData ret = new ServiceData();
		Gson gson = new Gson();
		ChwOrder chwOrder = (ChwOrder) gson.fromJson(param, ChwOrder.class);
		int i = this.chwOrderService.updateEntity(chwOrder.getChwPoHeader(), chwOrder.getLineList());
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(Integer.valueOf(i));
		return ret;
	}

	@RequestMapping(value = { "/delete.do" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST }, consumes = {
					"*/*" }, produces = { "application/json" })
	@ResponseBody
	@PermissionSecurity(name = "删除", module = "删除订单", btnfunc = "delete")
	public ServiceData delete(HttpServletRequest request, String id) throws Exception {
		ServiceData ret = new ServiceData();
		int i = this.chwOrderService.deleteEntity(id);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(Integer.valueOf(i));
		return ret;
	}
}